package com.example.capsule_chat0322.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.MessageBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.request.RoomRequest;
import com.example.capsule_chat0322.utils.AlterUtils;

/**
 * @Author zhuzhiming
 * @Date 2022/5/20 0:33
 */
public class AddSecretRoomActivity extends AppCompatActivity {

    private EditText room_name;
    private TextView sender_name;
    private EditText room_password;
    private EditText add_instruction;
    private Button add_commit;
    private final static int ADD_SECRET_ROOM = 1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_secret_room);
        initView();
    }


    private void initView() {

        room_name = findViewById(R.id.room_name);
        sender_name = findViewById(R.id.sender_name);
        room_password = findViewById(R.id.room_password);
        add_instruction = findViewById(R.id.add_instruction);
        add_commit = findViewById(R.id.add_commit);

        sender_name.setText("加入私密聊天室");


        add_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doCommit();
            }
        });

    }

    private void doCommit() {

        String userId = (String) GlobalAssistMap.getObject("userId");
        if (userId == null) {
            try {
                Thread.sleep(150);
                userId = (String) GlobalAssistMap.getObject("userId");
                if (userId == null) {
                    Thread.sleep(300);
                    userId = (String) GlobalAssistMap.getObject("userId");
                    if (userId == null) {
                        Thread.sleep(500);
                        userId = (String) GlobalAssistMap.getObject("userId");
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (userId == null) {
            return;
        }

        MessageBean messageBean = new MessageBean();
        String roomName = room_name.getText().toString();
        String password = room_password.getText().toString();
        if (password.length() == 0 || roomName.length() == 0) {
            return;
        }
        messageBean.setSenderId(userId);
        messageBean.setRoomName(roomName);
        messageBean.setField(GlobalAssistMap.getObject("field").toString());
        messageBean.setContent(password);
        RoomRequest.addSecretRoom(messageBean, new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                MessageBean messageBean = (MessageBean) message.obj;
                String content = messageBean.getContent();
                if (content.contains("加入成功")) {
                    GlobalAssistMap.putObject("secretRoomName", messageBean.getRoomName());
                    setResult(ADD_SECRET_ROOM);
                    finish();
                } else {
                    room_name.setText("");
                    room_password.setText("");
                    AlterUtils.alertByToastInMainThread(AddSecretRoomActivity.this, messageBean.getContent());
                }
                return true;
            }
        }));

    }
}
